import sys
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QIcon, QPalette, QPixmap, QBrush
from PyQt5.QtWidgets import QDialog, QPushButton, QApplication, QLabel, \
    QMessageBox, QWidget
from docx import Document

from cg_main.uistyle import setStyle


class DialogTemplate(QWidget):
    update_deviceNo = pyqtSignal(tuple)  # pyqt5 支持python3的str，没有Qstring

    def showdialog(self):
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)  # 隐藏边框
        dialog = QDialog()
        dialog.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
        dialog.resize(435, 400)
        no = QLabel(dialog)
        palette = QPalette()
        pix = QPixmap("./images/wordImage.png")
        palette.setBrush(QPalette.Background, QBrush(pix))
        dialog.setPalette(palette)
        dialog.setWindowIcon(QIcon('./images/python.jpg'))
        dialog.setStyleSheet('background-image')
        btn = QPushButton("生成模版", dialog)
        btn.setToolTip("在当前路径下生成WordTemplate.docx 模版文件，可在里面添加内容")
        btn.setObjectName('button')
        btn.setStyleSheet(setStyle())
        btn.setGeometry(QtCore.QRect(100, 350, 100, 50))
        dialog.setWindowTitle("word表格模版")
        no.setStyleSheet(setStyle())
        btn.clicked.connect(self.generateWordTable)
        dialog.setWindowModality(Qt.ApplicationModal)
        dialog.exec_()

    def generateWordTable(self):
        doc = Document()
        try:
            table = doc.add_table(rows=5, cols=5, style="Light Shading")
            title_cells = table.rows[0].cells
            titles = ['属性', '描述', '类型', '规则说明', '必填']
            for i in range(len(titles)):
                title_cells[i].text = titles[i]
            doc.save('WordTemplate.docx')
            QMessageBox.information(self, "提示", "创建模版成功", QMessageBox.Ok)
        except Exception as e:
            QMessageBox.information(self, "提示", "创建模版失败", QMessageBox.Ok)
            print(e)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = DialogTemplate()
    demo.showdialog()
    # demo.show()
    sys.exit(app.exec_())
